import axios from 'axios';
import { CreateKnowledgeBaseRequest, KnowledgeBase, KnowledgeBaseQuery, UpdateKnowledgeBaseRequest } from '../types/knowledgeBase';

// API基础URL
const API_BASE_URL = process.env.REACT_APP_API_BASE_URL || 'http://localhost:8080/api';

// 知识库API
export const knowledgeBaseApi = {
  /**
   * 获取所有知识库
   */
  getAll: async (params?: KnowledgeBaseQuery) => {
    const response = await axios.get(`${API_BASE_URL}/knowledge-bases`, { params });
    return response.data;
  },

  /**
   * 获取指定ID的知识库
   */
  getById: async (id: number) => {
    const response = await axios.get(`${API_BASE_URL}/knowledge-bases/${id}`);
    return response.data;
  },

  /**
   * 创建知识库
   */
  create: async (data: CreateKnowledgeBaseRequest) => {
    const response = await axios.post(`${API_BASE_URL}/knowledge-bases`, data);
    return response.data;
  },

  /**
   * 更新知识库
   */
  update: async (id: number, data: UpdateKnowledgeBaseRequest) => {
    const response = await axios.put(`${API_BASE_URL}/knowledge-bases/${id}`, data);
    return response.data;
  },

  /**
   * 删除知识库
   */
  delete: async (id: number) => {
    const response = await axios.delete(`${API_BASE_URL}/knowledge-bases/${id}`);
    return response.data;
  },

  /**
   * 获取知识库的文档数量
   */
  getDocumentCount: async (id: number) => {
    const response = await axios.get(`${API_BASE_URL}/knowledge-bases/${id}/document-count`);
    return response.data;
  },

  /**
   * 搜索知识库
   */
  search: async (query: string, params?: Omit<KnowledgeBaseQuery, 'searchTerm'>) => {
    const response = await axios.get(`${API_BASE_URL}/knowledge-bases/search`, { 
      params: { 
        ...params,
        query 
      } 
    });
    return response.data;
  }
};